#!/usr/bin/env php
<?php
/**
 * An SVN post-commit message sender.
 *
 * @version     $Id: sendcommitmsg.php 14 2008-02-15 21:29:56Z CXJohnson $
 * @author      Chris Johnson (tinpixel.com)
 * @copyright   2007, Chris Johnson
 * @see         License: GPLv3
 */

$svnlook = '/usr/bin/svnlook';
$linkticket = 'http://trac.example.com/ticket';
$linkchange = 'http://trac.example.com/changeset';
$mucdaemonhost = 'localhost';
$mucdaemonport = 6000;

$prognam = basename($argv[0]);

if ($argc != 3) {
    echo "$prognam: path rev (as from SVN post-commit hook)";
    exit (1);
}
$path = $argv[1];
$rev  = $argv[2];

// get commit info
$info = shell_exec(sprintf('%s -r %d info %s', $svnlook, $rev, $path));

list($author, $date, $num, $comment) = explode("\n", $info);
$comment = substr($comment, 0, 160);

// replace any #XXX style ticket numbers with a link to the ticket itself.
$remarks = preg_replace('/#(\d+)/', $linkticket .'/$1 ', $comment);

// format the desired message.  must end with a NL.
$message = "$linkchange/$rev by $author \n $remarks\n";  // trailing NL required

set_error_handler('mysockerrhandler');
$fp = fsockopen($mucdaemonhost, $mucdaemonport);
fwrite($fp, $message, strlen($message));
flush();
sleep(3);               // wait long enough for daemon to read all data
exit(0);

function mysockerrhandler($errno, $error, $errfile, $errline) {
    $msg = date('Y-m-d H:i:s') ." ($errno) $error on line $errline in file $errfile\n";
    @error_log($msg, 3, 'commitmsgerror.log');
    echo "$msg\n";
    exit(0);
}
?>
